#include "weatherwindow.h"
#include "ui_weatherwindow.h"
#include <QDateTime>
#include <QNetworkAccessManager>
#include "common.h"
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>

WeatherWindow::WeatherWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::WeatherWindow)
{
    ui->setupUi(this);

    //将QLabel放入容器中
    this->initVector();
    this->manager=new QNetworkAccessManager;
    connect(manager,&QNetworkAccessManager::finished,this,&WeatherWindow::weatherDate);

}

WeatherWindow::~WeatherWindow()
{
    delete ui;
}

void WeatherWindow::dateDeal()
{
    this->showTime();
    //定时器刷新时间
    this->timer=new QTimer;
    connect(this->timer,&QTimer::timeout,this,&WeatherWindow::showTime);
    this->timer->start(1000);
}

void WeatherWindow::initVector()
{
    this->times=new QVector<QLabel*>;
    this->times->append(ui->label_time1);
    this->times->append(ui->label_time2);
    this->times->append(ui->label_time3);
    this->times->append(ui->label_time4);
    this->times->append(ui->label_time5);
    this->times->append(ui->label_time6);
    this->times->append(ui->label_time7);

    this->icons=new QVector<QLabel*>;
    this->icons->append(ui->label_icon1);
    this->icons->append(ui->label_icon2);
    this->icons->append(ui->label_icon3);
    this->icons->append(ui->label_icon4);
    this->icons->append(ui->label_icon5);
    this->icons->append(ui->label_icon6);
    this->icons->append(ui->label_icon7);

    this->temperatures=new QVector<QLabel*>;
    this->temperatures->append(ui->label_temperature1);
    this->temperatures->append(ui->label_temperature2);
    this->temperatures->append(ui->label_temperature3);
    this->temperatures->append(ui->label_temperature4);
    this->temperatures->append(ui->label_temperature5);
    this->temperatures->append(ui->label_temperature6);
    this->temperatures->append(ui->label_temperature7);

    this->weathers=new QVector<QLabel*>;
    this->weathers->append(ui->label_weather1);
    this->weathers->append(ui->label_weather2);
    this->weathers->append(ui->label_weather3);
    this->weathers->append(ui->label_weather4);
    this->weathers->append(ui->label_weather5);
    this->weathers->append(ui->label_weather6);
    this->weathers->append(ui->label_weather7);
}

void WeatherWindow::initWeather()
{
    QString url=QString(WEATHER_API_URL).arg(WEATHER_API_VERSION).arg(WEATHER_APIID).arg(WEATHER_SECRET);
    manager->get(QNetworkRequest(QUrl(url)));
}

void WeatherWindow::init()
{
    this->dateDeal();
    //初始化天气
    this->initWeather();
}

void WeatherWindow::on_btn_back_clicked()
{
    this->parentWidget()->show();
    this->hide();
}

void WeatherWindow::showTime()
{
    //获取当前时间
    QDateTime datetime=QDateTime::currentDateTime();
    QString dateStr=datetime.toString("yyyy-MM-dd HH:mm:ss ddd");
    ui->label_time->setText(dateStr);
}

void WeatherWindow::weatherDate(QNetworkReply *reply)
{
    QByteArray data=reply->readAll();
    //解析json数据
    QJsonDocument doc=QJsonDocument::fromJson(data);
    QJsonObject obj=doc.object();
    //展示当前城市
    ui->btn_city->setText(obj.value("city").toString());
    //获取天气
    QJsonArray array=obj.value("data").toArray();
    for (int i=0; i<array.count(); i++) {
        QJsonObject value=array.at(i).toObject();
        //时间
        QString date=value.value("date").toString();
        QString week=value.value("week").toString();
        QString datatime=date+"\n"+week;
        //图标
        QString icon=value.value("wea_img").toString()+".png";
        QString iconUrl=":/images/weather/"+icon;
        QString styleSheet=QString("border-image: url(%1)").arg(iconUrl);
        //温度
        QString temMin=value.value("tem2").toString()+"℃";
        QString temMax=value.value("tem1").toString()+"℃";
        QString tem=temMin+"~"+temMax;
        //天气
        QString wea=value.value("wea").toString();

        times->at(i)->setText(datatime);
        icons->at(i)->setStyleSheet(styleSheet);
        weathers->at(i)->setText(tem);
        temperatures->at(i)->setText(wea);

        if(i==0){
            ui->label_icon->setStyleSheet(styleSheet);
            ui->label_weather->setText(wea);
            ui->label_temperature->setText(value.value("tem").toString()+"℃");
        }
    }
}

